package hydrap2p.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

public class StartupManagerTableModel extends DefaultTableModel {

	private static final ColumnContext[] columnArray = {
    	new ColumnContext("Module", String.class, false),
        new ColumnContext("Active?", Boolean.class, true),		//true == started, false == stopped
        new ColumnContext("Autostart?", Boolean.class, true),	//true == checked, false == unchecked 
    };
    private final Map<Integer, SwingWorker> swmap = new HashMap<Integer, SwingWorker>();
    private int number = 0;
    public void addModule(MockModule mm, SwingWorker worker) {
        Object[] obj = {mm.getName(), mm.getStatus(), mm.getAutostart()};
        super.addRow(obj);
        swmap.put(number, worker);
        number++;
    }
    public synchronized SwingWorker getSwingWorker(int identifier) {
        Integer key = (Integer)getValueAt(identifier, 0);
        return swmap.get(key);
    }
    public boolean isCellEditable(int row, int col) {
        return columnArray[col].isEditable;
    }
    public Class<?> getColumnClass(int modelIndex) {
        return columnArray[modelIndex].columnClass;
    }
    public int getColumnCount() {
        return columnArray.length;
    }
    public String getColumnName(int modelIndex) {
        return columnArray[modelIndex].columnName;
    }
    private static class ColumnContext {
        public final String  columnName;
        public final Class   columnClass;
        public final boolean isEditable;
        public ColumnContext(String columnName, Class columnClass, boolean isEditable) {
            this.columnName = columnName;
            this.columnClass = columnClass;
            this.isEditable = isEditable;
        }
    }
}

